package com.moore.Servlet;

import com.alibaba.fastjson2.JSON;
import com.moore.net.HttpRequest;
import com.moore.net.HttpResponse;
import com.moore.net.HttpServlet;
import com.moore.utils.SupportUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class SupportServlet extends HttpServlet {

    @Override
    public byte[] doGet(HttpRequest request, HttpResponse response) {
        String responseData = "";
        String type = request.getParams().get("type");
        switch (type) {
            case "get":
                responseData = JSON.toJSONString(SupportUtils.getText());
                break;
            case "insert":
                String user = request.getParams().get("user");
                String text;
                try {
                    text = URLDecoder.decode(request.getParams().get("text"), "utf-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                responseData = SupportUtils.insertText(user, text);
                break;
            case "clear":
                responseData = SupportUtils.clearText();
                break;
        }
        return responseData.getBytes();
    }
}
